package leetcode;

import java.util.*;

/***
 * 题目：49. 字母异位词分组
 * 链接：https://leetcode.cn/problems/group-anagrams/description/
 * 题解：哈希表
 */
public class GroupAnagrams {
    public static void main(String[] args) {
        List<List<String>> list = groupAnagrams(new String[]{"eat", "tea", "tan", "ate", "nat", "bat"});
        System.out.println(list);
    }
    public static List<List<String>> groupAnagrams(String[] strs) {
        Map<String, List<String>> map = new HashMap<String, List<String>>();
        for (String str : strs) {
            char[] array = str.toCharArray();
            Arrays.sort(array);
            String key = new String(array);
            List<String> list = map.getOrDefault(key, new ArrayList<String>());
            list.add(str);
            map.put(key, list);
        }
        System.out.println(map.values());
        return new ArrayList<List<String>>(map.values());

    }
}
